const express = require('express');
const cookieService = require('../service/cookie');
const {ApiResponse} = require("../model/response");

const router = express.Router();


// 添加cookie
router.post('/add', async (req, res) => {
    const {cookie} = req.body;
    try {
        const result = await cookieService.addCookie(cookie);
        res.json(ApiResponse.ok(200, "添加成功").sendData(result))
    } catch (e) {
        console.log(e)
        res.json(ApiResponse.fail(500, "添加失败"))
    }
})

// 获取所有cookie
router.get('/list', async (req, res) => {
    try {
        const result = await cookieService.getAllCookie();
        res.json(ApiResponse.ok(200, "获取成功").sendData(result))
    } catch (e) {
        console.log(e)
        res.json(ApiResponse.fail(500, "获取失败"))
    }
})

// 获取最后一个cookie
router.get('/last', async (req, res) => {
    try {
        const result = await cookieService.getLastCookie();
        res.json(ApiResponse.ok(200, "获取成功").sendData(result))
    } catch (e) {
        console.log(e)
        res.json(ApiResponse.fail(500, "获取失败"))
    }
})


module.exports = router;